import { uniq } from 'lodash'
import { getNickName } from '@/api/system/user'


export function nickName(dataListPath = 'dataList', iteratee = (item) => item.createBy) {

  return {
    data() {
      return {
        nickNameList: []
      }
    },
    computed: {
      labelNickName() {
        return (createBy) => this.nickNameList.find(item => `${item.userName}` === `${createBy}`)?.nickName || createBy
      }
    },
    watch: {
      [dataListPath]: function (val) {
        this.initNickName(val)
      }
    },
    methods: {
      initNickName(dataList) {
        return Promise.all(uniq(dataList.map(iteratee)).filter(createBy => createBy).map(createBy => getNickName(createBy)))
          .then(response => {
            this.nickNameList = response?.map(item => item.data ? item.data : {}) || []
            return Promise.resolve(response)
          })
      }
    }
  }
}
